import { createRouter, createWebHistory } from 'vue-router'
import { setUserStore } from '@/stores/modules/user'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      redirect: '/login',
      path: '/'
    },
    {
      path: '/login',
      name: 'Login',
      component: () => import('../views/login/login.vue')
    },
    {
      path: '/layout',
      name: 'Layout',
      component: () => import('../views/home/layout.vue'),
      children: [
        {
          redirect: '/layout/class',
          path: '/layout'
        },
        {
          path: '/layout/class',
          name: 'class',
          component: () => import('../views/home/class/class.vue')
        },
        {
          path: '/layout/manager',
          name: 'manager',
          component: () => import('../views/home/manager/manager.vue')
        },
        {
          path: '/layout/basic',
          name: 'basic',
          component: () => import('../views/home/basic/index.vue')
        },
        {
          path: '/layout/avatar',
          name: 'avatar',
          component: () => import('../views/home/avatar/index.vue')
        },
        {
          path: '/layout/update',
          name: 'update',
          component: () => import('../views/home/updatepsd/index.vue')
        }
      ]
    }
  ]
})
router.beforeEach((to) => {
  const store = setUserStore()
  if (!store.token && to.path !== '/login') return '/login'
  return true
})
export default router
